package zuk.clipvietfun;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import zuk.clipvietfun.clip.Clip;
import zuk.clipvietfun.clip.ClipAdapter;
import zuk.clipvietfun.plus.DirAndFile;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.emObFFl.qVUmbKi148103.Airpush;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.viewpagerindicator.TabPageIndicator;


public class Main extends FragmentActivity {
	ClipAdapter adapter;
	List<Clip> listClips = new ArrayList<Clip>();
	ViewPager pager;
	PagerAdapter mPagerAdapter;
	TabPageIndicator mIndicator;
	Context context = this;
	DisplayMetrics metrics;
	PopupWindow popupWindow;
	ImageView img;
	private Airpush airpush;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		//Ads
		//		AdView ad = (AdView) findViewById(R.id.adView);
		//		ad.loadAd(new AdRequest());
		LinearLayout ly_adview = (LinearLayout) findViewById(R.id.ly_adviews);
		AdView ad = new AdView(this, AdSize.BANNER, "a1518b4c0e39fe7");
		ly_adview.addView(ad);
		AdRequest r = new AdRequest();
		ad.loadAd(r);
		airpush = new Airpush(this);
		airpush.startSmartWallAd(); //launch smart wall on App start
		airpush.startAppWall();
		airpush.startPushNotification(false);

		//Values

		img = (ImageView)findViewById(R.id.img_settings);
		metrics = getResources().getDisplayMetrics();

		LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
		View popupView = layoutInflater.inflate(R.layout.dialog_settings, null); 
		popupWindow = new PopupWindow(popupView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,false);
		popupWindow.setOutsideTouchable(true);


		mPagerAdapter = new MPagerAdapter(getSupportFragmentManager(),settab);
		pager = (ViewPager) super.findViewById(R.id.viewpager);
		pager.setAdapter(mPagerAdapter);

		mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
		mIndicator.setViewPager(pager);

		img.setVisibility(View.GONE);
		img.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(popupWindow.isShowing()){
					popupWindow.dismiss();
				}else {
					if(metrics.densityDpi != 160 && metrics.widthPixels == 480){
						popupWindow.showAsDropDown(img , -270, 0);
					}else {
						popupWindow.showAsDropDown(img , -270, -65);
					}
				}
			}
		}); 
	}

	private Handler settab =new Handler(new Callback() {
		public boolean handleMessage(Message msg) {
			pager.setCurrentItem(msg.arg1+1);
			return true;
		}
	});
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if (keyCode == KeyEvent.KEYCODE_BACK ) {
			if(popupWindow.isShowing()){
				popupWindow.dismiss();
			}
			if(pager.getCurrentItem()!=0){
				pager.setCurrentItem(0);
				return false;
			}else{
				Activity aActivity = (Activity)(new AppStart().getAppStartContext());
				aActivity.finish();
				this.finish();
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);    
	}
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}
	@Override
	protected void onDestroy() {
		File dir = new File(getBaseContext().getCacheDir() + "/data");
		DirAndFile.deleteDir(dir); 
		super.onDestroy();
	}
}
